import {
  HostBinding,
  Component,
  OnInit,
  ViewChild,
  Input,
  Renderer2,
  forwardRef,
  Injector,
  Inject,
  DoCheck,
  OnDestroy
} from "@angular/core";
import {
  Observable,
  Subject,
  ReplaySubject,
  from,
  of,
  range,
  BehaviorSubject
} from "rxjs";
import { map, catchError, filter } from "rxjs/operators";
import { Router, ActivatedRoute } from "@angular/router";
import {
  AsyncValidator,
  NG_ASYNC_VALIDATORS,
  ValidationErrors,
  AbstractControl,
  FormBuilder,
  FormGroup,
  Validators,
  ControlValueAccessor,
  NG_VALUE_ACCESSOR,
  FormControl
} from "@angular/forms";

import { Store, select } from "@ngrx/store";
import { Actions } from "@ngrx/effects";
import * as fromUser from "@/user/store/reducers/index";
import * as fromRoot from "@/reducers/index";
import { UserActions } from "@/user/store/actions";
import { ILoginRequest } from "@/user/store/models/user";

import { PLATFORM_ID, APP_ID } from "@angular/core";
import { isPlatformServer } from "@angular/common";
import { LocaleService, TranslationService, Language } from "angular-l10n";

import { EhAnimateService } from "@/share/services/eh-animate.service";

@Component({
  selector: "app-my",
  templateUrl: "./my.component.html",
  styleUrls: ["./my.component.scss"],
  animations: [EhAnimateService.fadeInOut()]
})
export class MyComponent implements OnInit, OnDestroy {
  @HostBinding("style.height")
  public height = "100%";

  @Language() lang: string;

  user$ = this.store.pipe(
    select(fromRoot.user),
    filter(user => !!user)
  );

  user: any = {
    username: ""
  };

  sub$ = {
    userSub$: null
  };

  isServer: boolean;

  constructor(
    updates$: Actions,
    @Inject(PLATFORM_ID) private platformId: Object,
    private fb: FormBuilder,
    private renderer: Renderer2,
    @Inject("AccountService") private accountService,
    @Inject("StoreService") private storeService,
    @Inject("RegService") private regService,
    @Inject("UtilsService") private utilsService,
    @Inject("UserService") private userService,
    @Inject("MsgService") private msgService,
    @Inject("HasUsernameValidatorService") private hasUsernameValidatorService,
    private router: Router,
    private activeRouter: ActivatedRoute,
    private store: Store<fromUser.State | fromRoot.State>
  ) {
    this.isServer = isPlatformServer(this.platformId);
  }

  ngOnInit(): void {
    this.activeRouter.data.subscribe(e => {});
    this.sub$.userSub$ = this.user$.subscribe(e => {
      this.user = e;
    });
  }

  ngOnDestroy() {
    this.utilsService.unsub(this.sub$);
  }
}
